package com.mxpio.erp.workshop.service.impl;

import com.mxpio.erp.common.technology.entity.ItemMaterialProp;
import com.mxpio.erp.common.workshop.entity.DefectiveProductsNoticeLine;
import com.mxpio.erp.common.workshop.entity.ManufactureOrderItem;
import com.mxpio.erp.common.workshop.service.DefectiveProductsNoticeLineService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DefectiveProductsNoticeLineServiceImpl extends BaseServiceImpl<DefectiveProductsNoticeLine> implements DefectiveProductsNoticeLineService {
    @Override
    @Transactional
    public List<DefectiveProductsNoticeLine> listWithTransient(String noticeNo) {
        Criteria c = Criteria.create().addCriterion("noticeNo", Operator.EQ, noticeNo);
        List<DefectiveProductsNoticeLine> list = this.list(DefectiveProductsNoticeLine.class, c);
        if (list.size()>0){
            DefectiveProductsNoticeLine line1 = list.get(0);
            String originBizNo = line1.getOriginBizNo();
            List<ManufactureOrderItem> manufactureOrderItemList = JpaUtil.linq(ManufactureOrderItem.class).equal("bizNo", originBizNo).list();
            Map<Object, ManufactureOrderItem> lineNoMap = JpaUtil.index(manufactureOrderItemList, "lineNo");

            Set<String> itemCodeSet = list.stream().map(DefectiveProductsNoticeLine::getItemCode).collect(Collectors.toSet());
            List<ItemMaterialProp> materialProps =  JpaUtil.linq(ItemMaterialProp.class).equal("propType", "item").in("code", itemCodeSet).list();
            Map<String, ItemMaterialProp> materialPropMap = JpaUtil.index(materialProps,"code");

            for (DefectiveProductsNoticeLine noticeLine:list){
                String originBizLineNo = noticeLine.getOriginBizLineNo();
                if (lineNoMap.containsKey(originBizLineNo)&&lineNoMap.get(originBizLineNo)!=null){
                    ManufactureOrderItem orderItem = lineNoMap.get(originBizLineNo);
                    noticeLine.setMaterialBrand(orderItem.getMaterialBrand());
                    noticeLine.setMakeLength(orderItem.getMakeLength());
                    noticeLine.setMaterialType(orderItem.getMaterialType());
                    noticeLine.setMakeArea(orderItem.getMakeArea());
                    noticeLine.setMakeNum(orderItem.getMakeNum());
                }
                if (materialPropMap.containsKey(noticeLine.getItemCode())&&materialPropMap.get(noticeLine.getItemCode())!=null){
                    ItemMaterialProp itemMaterialProp = materialPropMap.get(noticeLine.getItemCode());
                    noticeLine.setMaterialDensity(itemMaterialProp.getMaterialDensity());
                    noticeLine.setMaterialDiameter(itemMaterialProp.getMaterialDiameter());
                    noticeLine.setMaterialThickness(itemMaterialProp.getMaterialThickness());
                }
            }
        }

        return list;
    }
}
